home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / FileInputStream.java < prev    next >
Text File  |  1998-09-22  |  8KB  |  223 lines

  1. /*
  2.  * @(#)FileInputStream.java    1.34 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * A file input stream is an input stream for reading data from a 
  19.  * <code>File</code> or from a <code>FileDescriptor</code>. 
  20.  *
  21.  * @author  Arthur van Hoff
  22.  * @version 1.34, 07/01/98
  23.  * @see     java.io.File
  24.  * @see     java.io.FileDescriptor
  25.  * @see        java.io.FileOutputStream
  26.  * @since   JDK1.0
  27.  */
  28. public
  29. class FileInputStream extends InputStream 
  30. {
  31.     /* File Descriptor - handle to the open file */
  32.     private FileDescriptor fd;
  33.     
  34.     /**
  35.      * Creates an input file stream to read from a file with the 
  36.      * specified name. 
  37.      *
  38.      * @param      name   the system-dependent file name.
  39.      * @exception  FileNotFoundException  if the file is not found.
  40.      * @exception  SecurityException      if a security manager exists, its
  41.      *               <code>checkRead</code> method is called with the name
  42.      *               argument to see if the application is allowed read access
  43.      *               to the file.
  44.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  45.      * @since      JDK1.0
  46.      */
  47.     public FileInputStream(String name) throws FileNotFoundException {
  48.     SecurityManager security = System.getSecurityManager();
  49.     if (security != null) {
  50.         security.checkRead(name);
  51.     }
  52.     try {
  53.         fd = new FileDescriptor();
  54.         open(name);
  55.     } catch (IOException e) {
  56.         throw new FileNotFoundException(name);
  57.     }
  58.     }
  59.     
  60.     /**
  61.      * Creates an input file stream to read from the specified 
  62.      * <code>File</code> object. 
  63.      *
  64.      * @param      file   the file to be opened for reading.
  65.      * @exception  FileNotFoundException  if the file is not found.
  66.      * @exception  SecurityException      if a security manager exists, its
  67.      *               <code>checkRead</code> method is called with the pathname
  68.      *               of this <code>File</code> argument to see if the
  69.      *               application is allowed read access to the file.
  70.      * @see        java.io.File#getPath()
  71.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  72.      * @since      JDK1.0
  73.      */
  74.     public FileInputStream(File file) throws FileNotFoundException {
  75.     this(file.getPath());
  76.     }
  77.  
  78.     /**
  79.      * Creates an input file stream to read from the specified file descriptor.
  80.      *
  81.      * @param      fdObj   the file descriptor to be opened for reading.
  82.      * @exception  SecurityException  if a security manager exists, its
  83.      *               <code>checkRead</code> method is called with the file
  84.      *               descriptor to see if the application is allowed to read
  85.      *               from the specified file descriptor.
  86.      * @see        java.lang.SecurityManager#checkRead(java.io.FileDescriptor)
  87.      * @since      JDK1.0
  88.      */
  89.     public FileInputStream(FileDescriptor fdObj) {
  90.     SecurityManager security = System.getSecurityManager();
  91.     if (fdObj == null) {
  92.         throw new NullPointerException();
  93.     }
  94.     if (security != null) {
  95.         security.checkRead(fdObj);
  96.     }
  97.     fd = fdObj;
  98.     }
  99.  
  100.     /**
  101.      * Opens the specified file for reading.
  102.      * @param name the name of the file
  103.      */
  104.     private native void open(String name) throws IOException;
  105.  
  106.     /**
  107.      * Reads a byte of data from this input stream. This method blocks 
  108.      * if no input is yet available. 
  109.      *
  110.      * @return     the next byte of data, or <code>-1</code> if the end of the
  111.      *             file is reached.
  112.      * @exception  IOException  if an I/O error occurs.
  113.      * @since      JDK1.0
  114.      */
  115.     public native int read() throws IOException;
  116.  
  117.  
  118.     /** 
  119.      * Reads a subarray as a sequence of bytes. 
  120.      * @param b the data to be written
  121.      * @param off the start offset in the data
  122.      * @param len the number of bytes that are written
  123.      * @exception IOException If an I/O error has occurred. 
  124.      */ 
  125.     private native int readBytes(byte b[], int off, int len) throws IOException;
  126.  
  127.     /**
  128.      * Reads up to <code>b.length</code> bytes of data from this input 
  129.      * stream into an array of bytes. This method blocks until some input 
  130.      * is available. 
  131.      *
  132.      * @param      b   the buffer into which the data is read.
  133.      * @return     the total number of bytes read into the buffer, or
  134.      *             <code>-1</code> if there is no more data because the end of
  135.      *             the file has been reached.
  136.      * @exception  IOException  if an I/O error occurs.
  137.      * @since      JDK1.0
  138.      */
  139.     public int read(byte b[]) throws IOException {
  140.     return readBytes(b, 0, b.length);
  141.     }
  142.  
  143.     /**
  144.      * Reads up to <code>len</code> bytes of data from this input stream 
  145.      * into an array of bytes. This method blocks until some input is 
  146.      * available. 
  147.      *
  148.      * @param      b     the buffer into which the data is read.
  149.      * @param      off   the start offset of the data.
  150.      * @param      len   the maximum number of bytes read.
  151.      * @return     the total number of bytes read into the buffer, or
  152.      *             <code>-1</code> if there is no more data because the end of
  153.      *             the file has been reached.
  154.      * @exception  IOException  if an I/O error occurs.
  155.      * @since      JDK1.0
  156.      */
  157.     public int read(byte b[], int off, int len) throws IOException {
  158.     return readBytes(b, off, len);
  159.     }
  160.  
  161.     /**
  162.      * Skips over and discards <code>n</code> bytes of data from the 
  163.      * input stream. The <code>skip</code> method may, for a variety of 
  164.      * reasons, end up skipping over some smaller number of bytes, 
  165.      * possibly <code>0</code>. The actual number of bytes skipped is returned.
  166.      *
  167.      * @param      n   the number of bytes to be skipped.
  168.      * @return     the actual number of bytes skipped.
  169.      * @exception  IOException  if an I/O error occurs.
  170.      * @since      JDK1.0
  171.      */
  172.     public native long skip(long n) throws IOException;
  173.  
  174.     /**
  175.      * Returns the number of bytes that can be read from this file input
  176.      * stream without blocking.
  177.      *
  178.      * @return     the number of bytes that can be read from this file input
  179.      *             stream without blocking.
  180.      * @exception  IOException  if an I/O error occurs.
  181.      * @since      JDK1.0
  182.      */
  183.     public native int available() throws IOException;
  184.  
  185.     /**
  186.      * Closes this file input stream and releases any system resources 
  187.      * associated with the stream. 
  188.      *
  189.      * @exception  IOException  if an I/O error occurs.
  190.      * @since      JDK1.0
  191.      */
  192.     public native void close() throws IOException;
  193.  
  194.     /**
  195.      * Returns the opaque file descriptor object associated with this stream.
  196.      *
  197.      * @return     the file descriptor object associated with this stream.
  198.      * @exception  IOException  if an I/O error occurs.
  199.      * @see        java.io.FileDescriptor
  200.      * @since      JDK1.0
  201.      */
  202.     public final FileDescriptor getFD() throws IOException {
  203.     if (fd != null) return fd;
  204.     throw new IOException();
  205.     }
  206.  
  207.     /**
  208.      * Ensures that the <code>close</code> method of this file input stream is
  209.      * called when there are no more references to it. 
  210.      *
  211.      * @exception  IOException  if an I/O error occurs.
  212.      * @see        java.io.FileInputStream#close()
  213.      * @since      JDK1.0
  214.      */
  215.     protected void finalize() throws IOException {
  216.     if (fd != null) {
  217.         if (fd != fd.in) {
  218.         close();
  219.         }
  220.     }
  221.     }
  222. }
  223.